package scau.erps.view.dashboard;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import org.jfree.chart.ChartPanel;


/**
 * 市场预测仪表盘
 * @author DCJ
 *
 */
public class MarketPanel  extends JPanel implements ActionListener{
	private static final long serialVersionUID = 1L;
	private JComboBox<String> itemCombo;
	private JButton selectBtn;
	private JPanel contentPanel,actionPanel,chartPanel;	
	private JTextArea infoTextArea;
	public MarketPanel(){				
		contentPanel = new JPanel();
  		contentPanel.setLayout(new BorderLayout());
		contentPanel.add(getActionPanel(),BorderLayout.NORTH);
		contentPanel.add(getChartPanel(),BorderLayout.CENTER);
		contentPanel.add(getInfoTextArea(),BorderLayout.SOUTH);
		this.add(contentPanel);
	}
	/**
	 * 切换不同统计指标Panel
	 * @return
	 */
	private JPanel getActionPanel(){
		String[] items = new String[]{"统计指标","产品","市场","价格","市场总量"};	
		itemCombo = new JComboBox<String>();
		for (int i = 0; i < items.length; i++) {
			itemCombo.addItem(items[i]);
		}
		selectBtn = new JButton("选择");		
		selectBtn.addActionListener(this);		
		actionPanel = new JPanel(); 
		actionPanel.add(itemCombo);		
		actionPanel.add(selectBtn);	
		return actionPanel;		
	}
	
	/**
	 * 图表Panel，默认加载产品市场预测图
	 * @return
	 */
	private JPanel getChartPanel(){		
		chartPanel = new JPanel(); 
		chartPanel.add(new ChartPanel(ProductChart.getChart()));	
		return chartPanel;
	}
	private void refreshChartPanel(ChartPanel chart){
		chartPanel.removeAll();
		chartPanel.add(chart);
		chartPanel.updateUI();
	}
	private JTextArea getInfoTextArea(){
		infoTextArea = new JTextArea();
		infoTextArea.setBorder(BorderFactory.createTitledBorder("说明"));
		infoTextArea.setText("系统未来订单预测，实际订单存在不确定性，仅供参考！");
		return infoTextArea;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==selectBtn){
			String item = null;
			if (itemCombo.getSelectedIndex() != 0) {
				item = (String) itemCombo.getSelectedItem();
			}
			if("产品".equals(item)){
				this.refreshChartPanel(new ChartPanel(ProductChart.getChart()));
				infoTextArea.setText("纵轴指该对应年份所有市场上该产品的数量总和；");
			}
			else if("市场".equals(item)){
				this.refreshChartPanel(new ChartPanel(RegionChart.getChart()));
				infoTextArea.setText("纵轴指某年份该市场上所有产品的数量总和；");
			}
			else if("价格".equals(item)){
				this.refreshChartPanel(new ChartPanel(PriceChart.getChart()));
				infoTextArea.setText("纵轴指某年份所有市场上该产品的价格平均值；");
			}
			else if("市场总量".equals(item)){
				this.refreshChartPanel(new ChartPanel(ScaleChart.getChart()));
				infoTextArea.setText("纵轴指某年份该市场上所有产品的市场总量（数量*价格）之和；");
			}
		}				
	}
}
